package com.example.springboot0507.fastjson;

import com.alibaba.fastjson2.*;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author 王博
 * @DateTime 2025-07-22 19:12
 */
@Slf4j
class FastJson {

    @Test
    void test6() {
        String jsonStr = "{\"code\":200,\"msg\":\"success\",\"person\":{\"birth\":\"2025-07-23 17:52:06\",\"fine\":true,\"id\":1,\"joinDate\":\"2025-07-23 17:52:06\",\"name\":\"张三\"}}";

        JSONObject jsonObject = JSON.parseObject(jsonStr);

        ResultVO<Person> resultVO = JSON.parseObject(jsonStr, new TypeReference<ResultVO<Person>>() {
        });

        log.info(resultVO.toString());
//        JSON.parseObject(jsonStr, new TypeReference<ResultVO>() {})
    }

    @Test
    public void test5() {
        Person person = new Person();
        person.setId(1L);
        person.setName("张三");
        person.setFine(true);
        person.setBirth(LocalDateTime.now());
        person.setJoinDate(new Date());

        ResultVO<Person> resultVO = new ResultVO<>();
        resultVO.setCode(200);
        resultVO.setMsg("success");
        resultVO.setPerson(person);

        String jsonString = JSON.toJSONString(resultVO);
        log.info(jsonString);

    }

    @Test
    void test4() {
        String jsonStr = "[{\"birth\":\"2025-07-23 13:30:01\",\"fine\":true,\"id\":1,\"joinDate\":\"2025-07-23 13:30:01\",\"name\":\"张三\"},{\"birth\":\"2025-07-23 13:30:01\",\"fine\":true,\"id\":1,\"joinDate\":\"2025-07-23 13:30:01\",\"name\":\"张三\"},{\"birth\":\"2025-07-23 13:30:01\",\"fine\":true,\"id\":1,\"joinDate\":\"2025-07-23 13:30:01\",\"name\":\"张三\"},{\"birth\":\"2025-07-23 13:30:01\",\"fine\":true,\"id\":1,\"joinDate\":\"2025-07-23 13:30:01\",\"name\":\"张三\"}]";

        List<Person> list = JSON.parseObject(jsonStr, new TypeReference<List<Person>>() {});
        List<Person> personList = JSON.parseArray(jsonStr, Person.class);
        JSONArray jsonArray = JSON.parseArray(jsonStr);
        log.info("{}",list);
    }

    @Test
    void test3() {
        List<Person> list = new ArrayList<>();

        Person person = new Person();
        person.setId(1L);
        person.setName("张三");
        person.setFine(true);
        person.setBirth(LocalDateTime.now());
        person.setJoinDate(new Date());

        list.add(person);
        list.add(person);
        list.add(person);
        list.add(person);

        String jsonString = JSON.toJSONString(list, JSONWriter.Feature.ReferenceDetection);
        String jsonString1 = JSON.toJSONString(list, JSONWriter.Feature.PrettyFormat);
        log.info(jsonString);
        log.info(jsonString1);
    }

    @Test
    void test2() {
        String jsonStr = "{\"birth\":\"2025-07-22 19:35:03\",\"id\":1,\"isFine\":true,\"joinDate\":\"2025-07-22 19:35:03\",\"name\":null}";
        Person person = JSON.parseObject(jsonStr, Person.class);
        log.info("{}", person);
    }

    @Test
    void test1() {
       Person person = new Person();
       person.setId(1L);
       person.setName("王博");
       person.setJoinDate(new Date());
       person.setBirth(LocalDateTime.now());
        person.setFine(true);

        String jsonString = JSON.toJSONString(person, JSONWriter.Feature.WriteNulls, JSONWriter.Feature.PrettyFormat, JSONWriter.Feature.WriteNonStringValueAsString);
        log.info(jsonString);
    }
}
